package com.mylove.okhttp;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.benyanyi.loglib.Jlog;
import com.benyanyi.okhttp.OkHttpUtil;
import com.benyanyi.okhttp.download.DownloadInfo;
import com.benyanyi.okhttp.listener.OnDownLoadObserver;
import com.benyanyi.okhttp.listener.OnOkHttpListener;
import com.benyanyi.permissionlib.PermissionCallBack;
import com.benyanyi.permissionlib.PermissionHelper;
import com.benyanyi.permissionlib.PermissionType;
import com.benyanyi.permissionlib.msg.FailureMsg;

import java.util.HashMap;
import java.util.Map;

/**
 * @author myLove
 * @time 2017/11/16 19:18
 * @e-mail mylove.520.y@gmail.com
 * @overview
 */

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.notification_contentview);
        setContentView(R.layout.activity_main);
        findViewById(R.id.but).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PermissionHelper.getInstance(MainActivity.this).hasPermission(0x11, new PermissionCallBack() {
                    @Override
                    public void onPermissionSuccess(int permissionCode) {
                        Intent intent = new Intent(MainActivity.this, DownloadActivity.class);
                        startActivity(intent);
                    }

                    @Override
                    public void onPermissionFailure(FailureMsg failureMsg) {

                    }

                    @Override
                    public void onPermissionComplete(int permissionCode) {

                    }
                }, Manifest.permission.WRITE_EXTERNAL_STORAGE);
            }
        });
        OkHttpUtil.getInstance(this).url("http://apitest.yanyi.online/app/update").getText(null).async(new OnOkHttpListener<Object>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onSuccess(Object message) {
                Jlog.d(message);
            }

            @Override
            public void onFailure(Throwable t) {

            }
        });
//        init();
//        PermissionHelper.getInstance(this).hasPermission(0x11, new PermissionCallBack() {
//            @Override
//            public void onPermissionSuccess(int permissionCode) {
//                downloadAPK();
//            }
//
//            @Override
//            public void onPermissionFailure(FailureMsg failureMsg) {
//
//            }
//
//            @Override
//            public void onPermissionComplete(int permissionCode) {
//
//            }
//        }, Manifest.permission.WRITE_EXTERNAL_STORAGE);
//        permissionHelper = new PermissionHelper(this, strings);
//        permissionHelper.hasPermission(new PermissionHelper.OnPermissionListener() {
//            @Override
//            public void onAllPermissionSuccess() {
////                init();
//                downloadAPK();
//            }
//
//            @Override
//            public void onAllPermissionFailure() {
//
//            }
//        });
//        String str = "<html></html>";
//        try {
////            JSONObject jsonObject = new JSONObject(str);
//            JsonElement jsonParser = JsonParser.parseString(str);
//            Jlog.v(jsonParser.isJsonArray());
//            Jlog.v(jsonParser.isJsonObject());
//            Jlog.v(jsonParser.isJsonPrimitive());
//            Jlog.v(jsonParser.isJsonNull());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }

    public void onClick(View view) {
        String[] strings = new String[]{PermissionType.STORAGE};
        PermissionHelper.getInstance(this).hasPermission(1, new PermissionCallBack() {
            @Override
            public void onPermissionSuccess(int permissionCode) {
                startActivity(new Intent(MainActivity.this, DownloadActivity.class));
            }

            @Override
            public void onPermissionFailure(FailureMsg failureMsg) {

            }

            @Override
            public void onPermissionComplete(int permissionCode) {

            }
        }, strings);
    }

    private void init() {
        String url = "http://test.zhcjrwang.com/api/Test/Index";
        Map<Object, Object> headerMap = new HashMap<>();
        headerMap.put("app_key", "XiangKeLai");
        String timeStamp = DateUtil.timeStamp();
        headerMap.put("timestamp", timeStamp);
        headerMap.put("sign", ("XiangKeLai1" + timeStamp).toUpperCase());
        Jlog.v(headerMap);
        Map<Object, Object> bodyMap = new HashMap<>();
        bodyMap.put("data", "+fAFfMCGsMZqeS+gyrZm1A==");
        Jlog.v(bodyMap);
        OkHttpUtil.getInstance(this).url(url, true).postText(headerMap, bodyMap).async(new OnOkHttpListener<Object>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onSuccess(Object message) {
                Jlog.v(message);
            }

            @Override
            public void onFailure(Throwable t) {
                Jlog.e(t.getMessage());
            }
        });
    }

    private void downloadAPK() {
//        JLog.init(true);
        String url = "http://apitest.yanyi.online/app/xiangyou.apk";
//        String filePath = "/dectector/dfu/";
////        String filePath = Environment.getExternalStorageDirectory().toString() + "/dectector/dfu/";
        OkHttpUtil.getInstance(this).url(url).download(true).start(new OnDownLoadObserver() {
            @Override
            public void onComplete() {

            }

            @Override
            public void onNext(DownloadInfo downloadInfo) {
                super.onNext(downloadInfo);
                Jlog.v(downloadInfo);
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                Jlog.e(e.getMessage());
            }
        });
//        OkHttpUtil.getInstance(this).downloadFile(url).downloads(filePath, new OnDownloadCallBack() {
//            @Override
//            public void onDownloading(int progress) {
//                JLog.d(progress + "");
//            }
//
//            @Override
//            public void onSuccess(String message) {
//                JLog.v(message);
//            }
//
//            @Override
//            public void onFailure(Throwable t) {
//                JLog.e(t.getMessage());
//            }
//        });
    }

}
